package com.stormragetech.demo.hbase.app;

import com.stormragetech.demo.hbase.spring.service.HBaseService;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
public class Launch {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-config.xml");
        context.registerShutdownHook();
        context.start();

        ExecutorService executor = Executors.newFixedThreadPool(5);

        HBaseService hBaseService = context.getBean(HBaseService.class);

        hBaseService.create();
        final AtomicLong count = new AtomicLong();
        Runnable exec = () -> {
            for (int i = 0; i < 5; i++) {
                try {
                    long c = count.incrementAndGet();
                    long start = System.currentTimeMillis();
                    String v = hBaseService.exec();
                    long end = System.currentTimeMillis() - start;
                    log.info("第[ {} ]次 | 用户名: {} | cost: {}ms", c, v, end);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        for (int i = 0; i < 10; i++) {
            executor.execute(exec);
        }
        executor.shutdown();
        while (!executor.isTerminated()) {
            if (executor.isTerminated()) {
                System.out.println("executor finished !!!");
            }
        }
        hBaseService.del();
        log.info("执行了[ {} ]次操作", count.get());

        System.exit(1);
    }
}
